package com.xhd.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.cache.CacheMgr;
import com.xhd.cache.UseCache;
import com.xhd.exception.SystemException;
import com.xhd.server.ServerConfig;
import com.xhd.xml.XMLSerializer;

public class BizObjConfig implements UseCache {
	 private static HashMap jm543 = null;
	  private static HashMap jm544 = null;
	  private static HashMap jm545 = null;
	  private HashMap jm546 = null;
	  private static final Log jm9;
	  private static boolean jm158;
	  static Class jm10;

	  static
	  {
	    Class tmp15_12 = jm10;
	    if (tmp15_12 == null)
	    {
	      
	      try
	      {
	         BizObjConfig.jm10 = Class.forName("com.sinocc.base.config.ActionConfig");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp15_12);
	    jm158 = false;
	  }

	  private BizObjConfig(HashMap paramHashMap)
	  {
	    this.jm546 = paramHashMap;
	    if (jm158)
	      return;
	    jm158 = true;
	    CacheMgr.registerCache(this);
	  }

	  public static void loadConfig()
	    throws SystemException
	  {
	    if (jm543 != null)
	      jm543.clear();
	    else
	      jm543 = new HashMap(512);
	    if (jm544 != null)
	      jm544.clear();
	    else
	      jm544 = new HashMap(512);
	    if (jm545 != null)
	      jm545.clear();
	    else
	      jm545 = new HashMap(512);
	    Object localObject2;
	    Object localObject1;
	    Object localObject3;
	    try
	    {
	      XMLSerializer localXMLSerializer = null;
	      Vector localVector2 = null;
	      Vector localVector3 = ServerConfig.getConfFiles("objconf");
	      int k;
	      if ((localVector3 != null) && (localVector3.size() > 0))
	      {
	        localVector2 = new Vector(512);
	        for (k = 0; k < localVector3.size(); ++k)
	        {
	          String str2 = (String)localVector3.get(k);
	          localObject2 = new File(str2);
	          if ((!(((File)localObject2).exists())) && (!(((File)localObject2).isFile())))
	            continue;
	          localObject1 = new FileInputStream((File)localObject2);
	           localXMLSerializer = new XMLSerializer((java.io.InputStream)localObject1, false);
	          localObject3 = localXMLSerializer.getCollection("objconf");
	          ((FileInputStream)localObject1).close();
	          if (localObject3 == null)
	            continue;
	          localVector2.addAll((Collection)localObject3);
	        }
	      }
	      if ((localVector2 != null) && (localVector2.size() > 0))
	      {
	        k = localVector2.size();
	        for (int l = 0; l < k; ++l)
	        {
	          localObject2 = new HashMap();
	          ((HashMap)localObject2).putAll((Map)localVector2.get(l));
	          localObject3 = (String)((HashMap)localObject2).get("tablename");
	          if ((localObject3 == null) || (((String)localObject3).length() < 1))
	          {
	            jm9.warn("Invalid object config: " + l);
	          }
	          else
	          {
	            localObject3 = ((String)localObject3).trim().toLowerCase();
	            if ((((String)localObject3).length() > 0) && (jm543.containsKey(localObject3)))
	              jm9.warn("Object config " + ((String)localObject3) + " repeat!");
	            BizObjConfig localEnpObjConfig = new BizObjConfig((HashMap)localObject2);
	            jm543.put(localObject3, localEnpObjConfig);
	            localObject3 = (String)((HashMap)localObject2).get("objtypeid");
	            if ((localObject3 == null) || (((String)localObject3).length() <= 0) || ("0".equals(localObject3)))
	              continue;
	            if (jm544.containsKey(localObject3))
	              jm9.warn("Obj config " + ((String)localObject3) + " repeat!");
	            jm544.put(localObject3, localEnpObjConfig);
	          }
	        }
	        jm9.info("Object config size: " + jm543.size());
	      }
	    }
	    catch (Exception localException)
	    {
	      jm9.fatal(localException);
	      throw new SystemException(10064, "objconf", localException.getMessage());
	    }
	    Vector localVector1 = ServerConfig.dataPool.getVector("cpcobjconf");
	    if ((localVector1 == null) || (localVector1.size() <= 0))
	      return;
	    int i = localVector1.size();
	    for (int j = 0; j < i; ++j)
	    {
	      localObject1 = (Map)localVector1.get(j);
	      String str1 = (String)((Map)localObject1).get("objtypeid");
	      if (str1 == null)
	        continue;
	      HashMap localHashMap = new HashMap(((Map)localObject1).size());
	      localHashMap.putAll((Map)localObject1);
	      localObject2 = new BizObjConfig(localHashMap);
	      jm545.put(str1, localObject2);
	      localObject3 = (String)((Map)localObject1).get("tablename");
	      if ((localObject3 == null) || (((String)localObject3).length() <= 0))
	        continue;
	      localObject3 = ((String)localObject3).trim().toLowerCase();
	      jm543.put(localObject3, localObject2);
	    }
	    jm9.info("cpcobjconf size: " + jm545.size());
	  }

	  public static String getPKField(String paramString)
	    throws SystemException
	  {
	    if (paramString == null)
	      return null;
	    BizObjConfig localEnpObjConfig = (BizObjConfig)jm543.get(paramString.toLowerCase());
	    if (localEnpObjConfig == null)
	      throw new SystemException(10059, paramString);
	    return localEnpObjConfig.getPK();
	  }

	  public static String findPKField(String paramString)
	  {
	    if (paramString == null)
	      return null;
	    BizObjConfig localEnpObjConfig = (BizObjConfig)jm543.get(paramString.toLowerCase());
	    if (localEnpObjConfig == null)
	      return null;
	    return localEnpObjConfig.getPK();
	  }

	  public static void refreshObjConf(Map paramMap)
	  {
	    String str = (String)paramMap.get("objtypeid");
	    if (str == null)
	      return;
	    BizObjConfig localEnpObjConfig = (BizObjConfig)jm545.get(str);
	    if (localEnpObjConfig != null)
	    {
	      localEnpObjConfig.jm546.putAll(paramMap);
	    }
	    else
	    {
	      HashMap localHashMap = new HashMap(paramMap.size());
	      localHashMap.putAll(paramMap);
	      localEnpObjConfig = new BizObjConfig(localHashMap);
	      jm545.put(str, localEnpObjConfig);
	      if ((localEnpObjConfig.getName() == null) || (localEnpObjConfig.getName().length() <= 0))
	        return;
	      jm543.put(localEnpObjConfig.getName(), paramMap);
	    }
	  }

	  public static Map getDefineById(int paramInt)
	  {
	    String str = String.valueOf(paramInt);
	    BizObjConfig localEnpObjConfig = (BizObjConfig)jm545.get(str);
	    if (localEnpObjConfig == null)
	      localEnpObjConfig = (BizObjConfig)jm544.get(str);
	    if (localEnpObjConfig == null)
	      return null;
	    return localEnpObjConfig.jm546;
	  }

	  public static BizObjConfig getObjConfig(int paramInt)
	  {
	    String str = String.valueOf(paramInt);
	    if (jm545.containsKey(str))
	      return ((BizObjConfig)jm545.get(str));
	    return ((BizObjConfig)jm544.get(str));
	  }

	  public static BizObjConfig getObjConfig(String paramString)
	  {
	    if (paramString == null)
	      return null;
	    paramString = paramString.toLowerCase();
	    BizObjConfig localEnpObjConfig = (BizObjConfig)jm543.get(paramString);
	    if (localEnpObjConfig == null)
	    {
	      BizObjClass localBizObjClass = BizObjClass.getInstance(paramString);
	      if (localBizObjClass != null)
	      {
	        HashMap localHashMap = new HashMap();
	        String str = localBizObjClass.findKeyField("id");
	        if (str != null)
	        {
	          localHashMap.put("pkfield", str);
	          str = localBizObjClass.findKeyField("code");
	          if (str != null)
	            localHashMap.put("codefield", str);
	          str = localBizObjClass.findKeyField("name");
	          if (str != null)
	            localHashMap.put("namefield", str);
	        }
	        else
	        {
	          jm9.debug("Class no PK: " + paramString);
	        }
	        localEnpObjConfig = new BizObjConfig(localHashMap);
	        jm543.put(paramString, localEnpObjConfig);
	      }
	      else
	      {
	        jm9.debug("Class no found: " + paramString);
	      }
	    }
	    return localEnpObjConfig;
	  }

	  public static BizObjConfig getObjConfig(Object paramObject)
	  {
	    if (paramObject == null)
	      return null;
	    String str = paramObject.getClass().getName();
	    int i = str.lastIndexOf(46);
	    if (i >= 0)
	      str = str.substring(i + 1);
	    return getObjConfig(str);
	  }

	  public static int getTypeByName(String paramString)
	  {
	    BizObjConfig localEnpObjConfig = getObjConfig(paramString);
	    if (localEnpObjConfig != null)
	      return localEnpObjConfig.getId();
	    return 0;
	  }

	  public int getId()
	  {
	    String str = (String)this.jm546.get("objtypeid");
	    if ((str != null) && (str.length() > 0))
	      return Integer.parseInt(str);
	    return 0;
	  }

	  public String getName()
	  {
	    return ((String)this.jm546.get("tablename"));
	  }

	  public String getTableName()
	  {
	    return ((String)this.jm546.get("tablename"));
	  }

	  public String getNameField()
	  {
	    return ((String)this.jm546.get("namefield"));
	  }

	  public String getCodeField()
	  {
	    return ((String)this.jm546.get("codefield"));
	  }

	  public String getTypeField()
	  {
	    return ((String)this.jm546.get("typefield"));
	  }

	  public boolean hasRev()
	  {
	    String str = (String)this.jm546.get("hasrev");
	    return ((str != null) && ("true".equalsIgnoreCase(str)));
	  }

	  public boolean hasPath()
	  {
	    String str = (String)this.jm546.get("haspath");
	    return ((str != null) && ("true".equalsIgnoreCase(str)));
	  }

	  public String getMainFields()
	  {
	    return ((String)this.jm546.get("mainfields"));
	  }

	  public String getSubjectField()
	  {
	    String str = (String)this.jm546.get("subjectfield");
	    if ((str == null) || (str.length() < 1))
	      str = (String)this.jm546.get("namefield");
	    return str;
	  }

	  public boolean isBill()
	  {
	    String str = (String)this.jm546.get("isbill");
	    return ((str != null) && ("true".equalsIgnoreCase(str)));
	  }

	  public String getJavaName()
	  {
	    return ((String)this.jm546.get("javaname"));
	  }

	  public String getBplName()
	  {
	    return ((String)this.jm546.get("bplname"));
	  }

	  public String getFormClass()
	  {
	    return ((String)this.jm546.get("formclass"));
	  }

	  public String getPK()
	  {
	    return ((String)this.jm546.get("pkfield"));
	  }

	  public String get(String paramString)
	  {
	    return ((String)this.jm546.get(paramString));
	  }

	  public boolean clearCache()
	  {
	    return false;
	  }

	  public int cacheSize()
	  {
	    int i = 0;
	    if (jm543 != null)
	      i += jm543.size();
	    if (jm544 != null)
	      i += jm544.size();
	    if (jm545 != null)
	      i += jm545.size();
	    return i;
	  }

	  public Object cacheData()
	  {
	    return jm543;
	  }
}
